using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using Core;
using System;


[CustomEditor(typeof(UnitView))]
public class UnitInspector : Editor
{
    private Unit owner;
    private Vector2 pos;

    private void OnEnable()
    {
        var unitView = target as UnitView;
        var component = World.GetComponent<UnitComponent>();
        if (component == null)
            return;

        component.TryGetUnit(unitView.target, out owner);
    }

    public override void OnInspectorGUI()
    {
        if(owner == null) return;

        EditorGUILayout.BeginScrollView(pos);
        {
            DrawNumeric();
            DrawBuff();
            DrawSkill();
        }
        EditorGUILayout.EndScrollView();

        EditorUtility.SetDirty(target);
    }

    void DrawNumeric()
    {
        var numericComponent = owner.GetComponent<NumericComponent>();
        if(numericComponent == null) return;

        var configComponent = World.GetComponent<ConfigComponent>();
        var loader = configComponent.GetLoader<NumbericConfigLoader>();
        EditorGUILayout.BeginVertical("HelpBox");
        {
            EditorGUILayout.LabelField($"numeric:");
            foreach (var key in numericComponent.Keys)
            {
                EditorGUILayout.LabelField($"name: {loader.Get(key).name}\tvalue: {numericComponent[key]}");
            }
        }
        EditorGUILayout.EndVertical();
    }

    void DrawBuff()
    {
        var buffComponent = owner.GetComponent<BuffComponent>();
        if (buffComponent == null)
            return;

        var configComponent = World.GetComponent<ConfigComponent>();
        EditorGUILayout.BeginVertical("HelpBox");
        {
            EditorGUILayout.LabelField($"buffs:");
            foreach (var buffId in buffComponent.GetAllBuff())
            {
                var buffConfig = configComponent.GetConfig<BuffConfig>(buffId);
                EditorGUILayout.LabelField($"id: {buffId}\ttype: {buffConfig.type}");
            }
        }
        EditorGUILayout.EndVertical();
    }

    void DrawSkill()
    {
        var skillComponent = owner.GetComponent<SkillComponent>();
        if (skillComponent == null)
            return;
        var configComponent = World.GetComponent<ConfigComponent>();
        EditorGUILayout.BeginVertical("HelpBox");
        {
            EditorGUILayout.LabelField($"skills:");
            foreach (var skill in skillComponent.GetSkills())
            {
                var skillId = skill.GetConfigId();
                var skillConfig = configComponent.GetConfig<SkillConfig>(skillId);
                EditorGUILayout.LabelField($"id: {skillId}\tbulletId: {skillConfig.bulletId}\tbuffs: {skillConfig.buffId}");
            }
        }
        EditorGUILayout.EndVertical();
    }
}
